package cn.itcast.usermanage.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import cn.itcast.usermanage.pojo.DatagridResult;
import cn.itcast.usermanage.pojo.PageResult;
import cn.itcast.usermanage.pojo.User;
import cn.itcast.usermanage.service.UserService;

/**   
*    
* 项目名称：usermanager   
* 类名称：UserControllerCRUD   
* 类描述：   
* 创建人：fw847   
* 创建时间：2018年3月8日 上午10:59:45   
* @version        
*/
@Controller("userControllerCRUD")
@RequestMapping("user")
public class UserControllerCRUD {
	
	@Autowired
	private UserService userService;
	
	
/*	@RequestMapping(method=RequestMethod.DELETE)
	@ResponseBody
	public PageResult userDelete(@RequestParam("ids") String ids){
		
		try {
			String[] userIds = StringUtils.split(ids, ",");
			
			userService.deteleUserById(userIds);
			
			return PageResult.ok();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return PageResult.error();
		}
	}*/
	@RequestMapping(method=RequestMethod.DELETE)
	public ResponseEntity<Void> userDeleteSqlForeach(@RequestParam("ids") Long[] ids){
		
		try {
			for (Long long1 : ids) {
				System.out.println(long1);
			}
			userService.deteleUserByIdSqlForeach(ids);
			
			return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}

 /**
  * @method  
  * @description 描述方法的作用: REST的查询用户
  * @date:  
  * @author: 封威
  * @param
  * @return 
  */
	@RequestMapping(value = "{id}",method = RequestMethod.GET)
	public ResponseEntity<User> queryByUserId(@PathVariable("id") Long id){
		try {
			User user = userService.queryByUserId(id);
			if (null != user){
				ResponseEntity<User> entity = new ResponseEntity<>(user, HttpStatus.OK);
				return entity;
			}else {
				return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new ResponseEntity<User>(HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	
	
	/**
	* @Title: insertUser
	* @Description: TODO(这是一个  增加客户)
	* @param @param user
	* @param @return    参数
	* @return ResponseEntity<Void>    返回类型
	* @throws
	*/
	@RequestMapping(method=RequestMethod.POST)
	public ResponseEntity<Void> insertUser(User user){
		try {
			userService.insertUser(user);
			return ResponseEntity.status(HttpStatus.CREATED).build();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	

	
	/**
	* @Title: updateUser
	* @Description: TODO(这是一个修改数据  )
	* @param @param user
	* @param @return    参数
	* @return ResponseEntity<Void>    返回类型
	* @throws
	*/
	@RequestMapping(method=RequestMethod.PUT)
	public ResponseEntity<Void> updateUser(User user){
		try {
			userService.updateUser(user);
			return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);

	}


	
}
